package com.steak.elasticsearch.service;

import com.steak.elasticsearch.entity.Item;
import com.steak.elasticsearch.respository.ItemRepository;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.search.aggregations.Aggregation;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.Aggregations;
import org.elasticsearch.search.aggregations.bucket.terms.StringTerms;
import org.elasticsearch.search.aggregations.support.ValuesSourceAggregationBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.core.ResultsExtractor;
import org.springframework.data.elasticsearch.core.aggregation.AggregatedPage;
import org.springframework.data.elasticsearch.core.query.FetchSourceFilter;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

@Service
public class ItemService {
    @Autowired
    private ItemRepository itemRepository;
    @Autowired
    private ElasticsearchTemplate elasticsearchTemplate;

    //查询所有
    public Iterable<Item> queryAll() {
        //升序,相应降序为dscending
        Iterable<Item> items = this.itemRepository.findAll(Sort.by("price").ascending());
        return items;
    }

    //聚合
    public Map<String, Integer> polymerizationQuery() {
        String aggName = "popularBrand";
        NativeSearchQueryBuilder queryBuilder = new NativeSearchQueryBuilder();
        //聚合
        queryBuilder.addAggregation(AggregationBuilders.terms("popularBrand").field("brand"));
        //查询并返回带聚合结果
        AggregatedPage<Item> result = elasticsearchTemplate.queryForPage(queryBuilder.build(), Item.class);
        //解析聚合
        Aggregations aggregations = result.getAggregations();
        //获取指定名称的聚合
        StringTerms terms = aggregations.get(aggName);
        //获取桶
        List<StringTerms.Bucket> buckets = terms.getBuckets();
        //遍历打印
        Map<String, Integer> map = new HashMap<>();
        for (StringTerms.Bucket bucket : buckets) {
            map.put(bucket.getKeyAsString(), (int) bucket.getDocCount());
            System.out.println("key = " + bucket.getKeyAsString());
            System.out.println("DocCount = " + bucket.getDocCount());
        }
        return map;
    }

    public Map<String, List<Item>> queryByTitle(String title) {
        List<Item> list = this.itemRepository.findByTitleLike(title);
        List<Item> items = new LinkedList<>();
        Map<String , List<Item>> map = new HashMap<>();
        for (Item item : list) {
            items.add(item);
            System.out.println(item);
        }
        map.put("item",items);
        return map;
    }

    public Map<String, List<Item>> queryByBrand(String brand) {
        List<Item> list = itemRepository.findByBrandLike(brand);
        List<Item> items = new LinkedList<>();
        Map<String,List<Item>> map = new HashMap<>();
        for (Item item : list) {
            items.add(item);
        }
        map.put("item",items);
        return map;
    }
}
